package com.liucxu.zimuzu.View.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liucxu.zimuzu.Model.Bean.ResourceDetailsInfoBean;
import com.liucxu.zimuzu.Model.Bean.ResourceDetailsInfoEntity;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.Utils.DisplayUtil;
import com.liucxu.zimuzu.Utils.GlideUtils;
import com.liucxu.zimuzu.Utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 今日更新adapter
 * Created by liukanshan on 2016/12/24.
 */

public class UpdateAdapter extends BaseAdapter {

    private List<ResourceDetailsInfoBean> datas = new ArrayList<>();
    private Context mContext;

    public UpdateAdapter(Context context, List<ResourceDetailsInfoBean> infos) {
        if (infos != null) {
            datas.clear();
            datas.addAll(infos);
        }
        mContext = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i).getData();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_hot, null);
        }
        initViews(position, convertView);
        return convertView;
    }

    /**
     * @param position
     * @param convertView
     */
    private void initViews(int position, View convertView) {
        ResourceDetailsInfoEntity entity = datas.get(position).getData();
        RelativeLayout card_content_rl = ViewHolderUtils.get(convertView, R.id.card_content_rl);
        ImageView adapter_hot_imager = ViewHolderUtils.get(convertView, R.id.adapter_hot_imager);
        TextView adapter_hot_title = ViewHolderUtils.get(convertView, R.id.adapter_hot_title);
        GlideUtils.getInstance().LoadSimpleContextRoundBitmap(mContext, entity.getPoster(), adapter_hot_imager, DisplayUtil.px2dip(mContext, 14));
        adapter_hot_title.setText(entity.getCnname());
        //点击事件，不好意思写在这个地方
        card_content_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        TextView adapter_en_name = ViewHolderUtils.get(convertView, R.id.adapter_en_name);
        adapter_en_name.setText(entity.getEnname());
        TextView resource_desc = ViewHolderUtils.get(convertView, R.id.resource_desc);
        resource_desc.setText("说明：" + entity.getRemark());
        TextView resource_type = ViewHolderUtils.get(convertView, R.id.resource_type);
        resource_type.setText("类型：" + entity.getCategory());
        TextView resource_score = ViewHolderUtils.get(convertView, R.id.resource_score);
        resource_score.setText("地区：" + entity.getArea());
    }

    /**
     * 刷新数据
     * @param beans
     */
    public void setDatas(List<ResourceDetailsInfoBean> beans) {
        if (beans != null) {
            datas.clear();
            datas.addAll(beans);
            notifyDataSetChanged();
        }
    }
}
